<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Admin;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Crypt;

class UserController extends AdminController
{
    protected $title = '账号管理';
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {
          //  $grid->column('id')->sortable();
            $grid->column('email','账号');
            $grid->column('password','密码')->display(function(){
                return '*****';
            });
            $grid->column('day','天数');
            $grid->column('expired_at','过期时间')->display(function($x){
                if($x&&strtotime($x)<time()){
                    return '<span class="label" style="background:red">已过期</span>'; 
                }else{
                    if($x==0){
                        return '<span class="label" style="background:#586cb1">未使用</span>'; 
                    }
                 
                     return '<span class="label" style="background:green">'.$x.'</span>'; 
                }
           
            });

            $grid->column('name','绑定设备');
            $grid->column('remember_token','所在地');
            $grid->addcolumn('地图')->display(function(){
                return "<a href='https://restapi.amap.com/v3/ip?ip=".$this->remember_token."&output=json&key=cf3b4b54a1aa8073ca722f384a4547c5' target='_blank'>查看</a>";
            });
            $grid->export(['id' => 'ID', 'email' => '账号']);
            $grid->column('created_at','创建时间');
            $grid->column('updated_at','活动时间')->sortable();
            $grid->model()->orderBy('id', 'desc');
           // $grid->column('updated_at')->sortable();
            $grid->disableBatchDelete();
            $grid->disableRefreshButton();
            if(!Admin::user()->isAdministrator()){
                $grid->disableActions();
                $grid->disableCreateButton();
                $grid->model()->where('uid',Admin::user()->id);
            }else{
                  $grid->column('uid','所属用户')->display(function($x){
                      if($x)
                            return "<a href='/admin/auth/users/$x' target='_blank'>查看</a>";
                      else
                           return "管理员";
                 });
            }
          
            $grid->disableViewButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('email','账号');
                $filter->equal('uid','代理ID');
                $filter->equal('name','绑定设备');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('email');
            $show->field('email_verified_at');
            $show->field('password');
            $show->field('remember_token');
            $show->field('created_at');
            $show->field('updated_at');
            
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
           // $form->text('name');
            $form->text('email','账号')->default(time());
          //  $form->text('email_verified_at');
          //  $form->text('remember_token');
            
           // $form->display('updated_at');
            if ($form->isEditing()) {
                $form->datetime('expired_at','失效时间');
            }else{
                $form->text('password','密码')->default(time());
                $form->number('day','有效天数');
                        $form->number('uid','所属用户')->default(0);
            }
            $form->display('created_at');
            $form->footer(function ($footer) {
                $footer->disableViewCheck();
                $footer->disableEditingCheck();
                $footer->disableReset();
                $footer->disableCreatingCheck();
            });
            $form->saving(function (Form $form) {
                    $form->password=bcrypt($form->password);
                return $form;
            });
        });
    }
  
}
